home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / comm / tcp / amitcptelnetf.lha / amitcp_telnet+ftp / ftp / ruserpass.c < prev   
C/C++ Source or Header  |  1993-07-03  |  6KB  |  269 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)ruserpass.c    5.1 (Berkeley) 3/1/89";
  20. #endif /* not lint */
  21.  
  22. #include <sys/types.h>
  23. #include <stdio.h>
  24. //#include <utmp.h>
  25. #include <ctype.h>
  26. #include <sys/stat.h>
  27. #include <errno.h>
  28. #include "ftp_var.h"
  29.  
  30. char    *renvlook(), *malloc(), *getenv(), *getpass(), *getlogin();
  31. static    FILE *cfile;
  32.  
  33. #ifndef MAXHOSTNAMELEN
  34. #define MAXHOSTNAMELEN 64
  35. #endif
  36.  
  37. #define    DEFAULT    1
  38. #define    LOGIN    2
  39. #define    PASSWD    3
  40. #define    ACCOUNT 4
  41. #define MACDEF  5
  42. #define    ID    10
  43. #define    MACH    11
  44.  
  45. static char tokval[100];
  46.  
  47. static struct toktab {
  48.     char *tokstr;
  49.     int tval;
  50. } toktab[]= {
  51.     "default",    DEFAULT,
  52.     "login",    LOGIN,
  53.     "password",    PASSWD,
  54.     "passwd",    PASSWD,
  55.     "account",    ACCOUNT,
  56.     "machine",    MACH,
  57.     "macdef",    MACDEF,
  58.     0,        0
  59. };
  60.  
  61. ruserpass(host, aname, apass, aacct)
  62.     char *host, **aname, **apass, **aacct;
  63. {
  64. #if 0
  65.     char *hdir, buf[BUFSIZ], *tmp;
  66.     char myname[MAXHOSTNAMELEN], *mydomain;
  67.     int t, i, c, usedefault = 0;
  68.     struct stat stb;
  69.     extern int errno;
  70.  
  71.     hdir = getenv("HOME");
  72.     if (hdir == NULL)
  73.         hdir = ".";
  74.     (void) sprintf(buf, "%s/.netrc", hdir);
  75.     cfile = fopen(buf, "r");
  76.     if (cfile == NULL) {
  77.         if (errno != ENOENT)
  78.             perror(buf);
  79.         return(0);
  80.     }
  81.     if (gethostname(myname, sizeof(myname)) < 0)
  82.         myname[0] = '\0';
  83.     if ((mydomain = index(myname, '.')) == NULL)
  84.         mydomain = "";
  85. next:
  86.     while ((t = token())) switch(t) {
  87.  
  88.     case DEFAULT:
  89.         usedefault = 1;
  90.         /* FALL THROUGH */
  91.  
  92.     case MACH:
  93.         if (!usedefault) {
  94.             if (token() != ID)
  95.                 continue;
  96.             /*
  97.              * Allow match either for user's input host name
  98.              * or official hostname.  Also allow match of 
  99.              * incompletely-specified host in local domain.
  100.              */
  101.             if (strcasecmp(host, tokval) == 0)
  102.                 goto match;
  103.             if (strcasecmp(hostname, tokval) == 0)
  104.                 goto match;
  105.             if ((tmp = index(hostname, '.')) != NULL &&
  106.                 strcasecmp(tmp, mydomain) == 0 &&
  107.                 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
  108.                 tokval[tmp - hostname] == '\0')
  109.                 goto match;
  110.             if ((tmp = index(host, '.')) != NULL &&
  111.                 strcasecmp(tmp, mydomain) == 0 &&
  112.                 strncasecmp(host, tokval, tmp - host) == 0 &&
  113.                 tokval[tmp - host] == '\0')
  114.                 goto match;
  115.             continue;
  116.         }
  117.     match:
  118.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  119.  
  120.         case LOGIN:
  121.             if (token())
  122.                 if (*aname == 0) { 
  123.                     *aname = malloc((unsigned) strlen(tokval) + 1);
  124.                     (void) strcpy(*aname, tokval);
  125.                 } else {
  126.                     if (strcmp(*aname, tokval))
  127.                         goto next;
  128.                 }
  129.             break;
  130.         case PASSWD:
  131.             if (strcmp(*aname, "anonymous") &&
  132.                 fstat(fileno(cfile), &stb) >= 0 &&
  133.                 (stb.st_mode & 077) != 0) {
  134.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  135.     fprintf(stderr, "Remove password or correct mode.\n");
  136.                 goto bad;
  137.             }
  138.             if (token() && *apass == 0) {
  139.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  140.                 (void) strcpy(*apass, tokval);
  141.             }
  142.             break;
  143.         case ACCOUNT:
  144.             if (fstat(fileno(cfile), &stb) >= 0
  145.                 && (stb.st_mode & 077) != 0) {
  146.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  147.     fprintf(stderr, "Remove account or correct mode.\n");
  148.                 goto bad;
  149.             }
  150.             if (token() && *aacct == 0) {
  151.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  152.                 (void) strcpy(*aacct, tokval);
  153.             }
  154.             break;
  155.         case MACDEF:
  156.             if (proxy) {
  157.                 (void) fclose(cfile);
  158.                 return(0);
  159.             }
  160.             while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
  161.             if (c == EOF || c == '\n') {
  162.                 printf("Missing macdef name argument.\n");
  163.                 goto bad;
  164.             }
  165.             if (macnum == 16) {
  166.                 printf("Limit of 16 macros have already been defined\n");
  167.                 goto bad;
  168.             }
  169.             tmp = macros[macnum].mac_name;
  170.             *tmp++ = c;
  171.             for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
  172.                 !isspace(c); ++i) {
  173.                 *tmp++ = c;
  174.             }
  175.             if (c == EOF) {
  176.                 printf("Macro definition missing null line terminator.\n");
  177.                 goto bad;
  178.             }
  179.             *tmp = '\0';
  180.             if (c != '\n') {
  181.                 while ((c=getc(cfile)) != EOF && c != '\n');
  182.             }
  183.             if (c == EOF) {
  184.                 printf("Macro definition missing null line terminator.\n");
  185.                 goto bad;
  186.             }
  187.             if (macnum == 0) {
  188.                 macros[macnum].mac_start = macbuf;
  189.             }
  190.             else {
  191.                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  192.             }
  193.             tmp = macros[macnum].mac_start;
  194.             while (tmp != macbuf + 4096) {
  195.                 if ((c=getc(cfile)) == EOF) {
  196.                 printf("Macro definition missing null line terminator.\n");
  197.                     goto bad;
  198.                 }
  199.                 *tmp = c;
  200.                 if (*tmp == '\n') {
  201.                     if (*(tmp-1) == '\0') {
  202.                        macros[macnum++].mac_end = tmp - 1;
  203.                        break;
  204.                     }
  205.                     *tmp = '\0';
  206.                 }
  207.                 tmp++;
  208.             }
  209.             if (tmp == macbuf + 4096) {
  210.                 printf("4K macro buffer exceeded\n");
  211.                 goto bad;
  212.             }
  213.             break;
  214.         default:
  215.     fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  216.             break;
  217.         }
  218.         goto done;
  219.     }
  220. done:
  221.     (void) fclose(cfile);
  222.     return(0);
  223. bad:
  224.     (void) fclose(cfile);
  225.     return(-1);
  226. #else
  227.     return 0;
  228. #endif
  229. }
  230.  
  231. static
  232. token()
  233. {
  234.     char *cp;
  235.     int c;
  236.     struct toktab *t;
  237.  
  238.     if (feof(cfile))
  239.         return (0);
  240.     while ((c = getc(cfile)) != EOF &&
  241.         (c == '\n' || c == '\t' || c == ' ' || c == ','))
  242.         continue;
  243.     if (c == EOF)
  244.         return (0);
  245.     cp = tokval;
  246.     if (c == '"') {
  247.         while ((c = getc(cfile)) != EOF && c != '"') {
  248.             if (c == '\\')
  249.                 c = getc(cfile);
  250.             *cp++ = c;
  251.         }
  252.     } else {
  253.         *cp++ = c;
  254.         while ((c = getc(cfile)) != EOF
  255.             && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  256.             if (c == '\\')
  257.                 c = getc(cfile);
  258.             *cp++ = c;
  259.         }
  260.     }
  261.     *cp = 0;
  262.     if (tokval[0] == 0)
  263.         return (0);
  264.     for (t = toktab; t->tokstr; t++)
  265.         if (!strcmp(t->tokstr, tokval))
  266.             return (t->tval);
  267.     return (ID);
  268. }
  269.